//
//  HomeViewController.m
//  JunZeWristband
//
//  Created by Kendama on 2017/8/17.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "HomeViewController.h"
#import "UIViewController+LGSideMenuController.h"
#import "HomeView.h"
#import "WebViewController.h"
#import "DeviceListViewController.h"
#import "PublicGetADListApi.h"
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>

@interface HomeViewController ()

@property(nonatomic,strong) HomeView *containerView;
@property(nonatomic,strong)CTCallCenter *callCenter;  //必须在这里声明，要不不会回调block

@end

@implementation HomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 监听电话
    _callCenter = [[CTCallCenter alloc] init];
    _callCenter.callEventHandler=^(CTCall* call){
        if([call.callState isEqualToString:CTCallStateDisconnected])
        {
            NSLog(@"Call has been disconnected");
            if (EXRACT_BOOL([MAC stringByAppendingString:CallRemind]))
            {
                DTCDeviceModel *device = [DTCBluetoothManager shareInstance].connectedDeviceList.lastObject;
                NSData *sendData = [NSString convertHexStrToData:offCallPushCode];
                [[DTCBlueToothSendData sharedInstance] sendDataByData:sendData andInfo:device];
                
            }
        }
        else if([call.callState isEqualToString:CTCallStateConnected])
        {
            NSLog(@"Callhasjustbeen connected");
        }
        else if([call.callState isEqualToString:CTCallStateIncoming])
        {

            NSLog(@"Call is incoming");
        }
        else if([call.callState isEqualToString:CTCallStateDialing])
        {

            NSLog(@"Call is Dialing");
        }
        else
        {
            NSLog(@"Nothing is done");
        }
    };
    
    [self loadADImageView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.sideMenuController.leftViewEnabled = YES;
//    [self loadADImageView];

}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.sideMenuController.leftViewEnabled = NO;

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    
}

- (void)refreshBaseControlValue
{
   
}

#pragma mark - 点击事件
- (void)onLeftButtonClick:(id)sender
{
    [self.sideMenuController showLeftViewAnimated:YES completionHandler:nil];
}

- (void)onRightButtonClick:(id)sender
{
    WebViewController *vc = [[WebViewController alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://www.junzebaby.com/"];
    vc.url = url;
    vc.webViewType = Store;
    [self.navigationController pushViewController:vc animated:YES];
//    [ShareView showShareViewWithClickShareBlock:^(NSInteger shareIndex) {
//        [DTCSharePlatformManager shareWithImage:[DTCSharePlatformManager getImageByView:self.view] Type:shareIndex];
//    }];
}

- (void)didClickWristbandView
{
    if ([DTCBluetoothManager shareInstance].blueState == CBManagerStatePoweredOn)
    {
        DeviceListViewController *vc = [[DeviceListViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:MyNSLocalizedString(@"请打开蓝牙")];
    }
    

}

- (void)didClickOtherView
{
    [SVProgressHUD showInfoWithStatus:MyNSLocalizedString(@"正在开发中...")];
}

- (void)didClickAboutWe
{
    WebViewController *vc = [[WebViewController alloc] init];
    vc.webViewType = AboutWe;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 事件处理
- (void)loadADImageView{
    PublicGetADListApi *api = [[PublicGetADListApi alloc] init];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary *dic = [request responseJSONObject];
        if ([dic[@"errcode"] intValue] == 0) {
            NSArray *array = dic[@"data"];
            NSString *domain = dic[@"domain"];
            NSMutableArray *data = [[NSMutableArray alloc] init];
            for (NSDictionary *dict in array) {
                NSString *urlString = [NSString stringWithFormat:@"%@/%@",domain,dict[@"image"]];
                [data addObject:urlString];
            }
            
            self.containerView.cycleScrollView.imageURLStringsGroup = data;
            
        } else {
            [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
        }
    } failure:^(YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showErrorWithStatus:MyNSLocalizedString(@"网络无法连接")];

    }];

//    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
//        
//        
//    } failure:nil];
}

#pragma mark - 代理


#pragma mark - 通知处理

#pragma mark - 初始化
- (void)setNavigationStyle
{
    
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_my"]];
    [self setRightButtonImage:[UIImage imageNamed:@"nav_icon_shop"]];
    [self setNavigationTitle:MyNSLocalizedString(@"产品列表") titleColor:[UIColor whiteColor]];
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.left.right.bottom.equalTo(superView);
    }];
}

- (HomeView *)containerView
{
    if(!_containerView)
    {
        UIView *superView = self.view;
        _containerView = [[HomeView alloc]init];
        [superView addSubview:_containerView];
        UITapGestureRecognizer*wristbandTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickWristbandView)];
        [_containerView.wristbandView addGestureRecognizer:wristbandTapGesture];
        UITapGestureRecognizer*otherTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickOtherView)];
        [_containerView.otherView addGestureRecognizer:otherTapGesture];
        [_containerView.meetButton addTarget:self action:@selector(didClickAboutWe) forControlEvents:UIControlEventTouchUpInside];
    }
    return _containerView;
}


@end
